package cn.yunyichina.provider.merchant.util;

import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.iface.entity.merchant.NotifyScene;
import cn.yunyichina.provider.merchant.dao.MerchantConfigDao;
import cn.yunyichina.provider.merchant.dao.MerchantMessageDao;
import cn.yunyichina.provider.merchant.dao.MethodRouterDao;
import cn.yunyichina.provider.merchant.entity.MerchantConfig;
import cn.yunyichina.provider.merchant.entity.MerchantMessage;
import cn.yunyichina.provider.merchant.entity.MethodRouter;
import cn.yunyichina.utils.commons.DateUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kevin on 2017/12/7.
 */
public class DataUtil {

    /**
     * 从数据库查询方法路由存进map，方便读取
     * @return
     */
    public static Map<String,MethodRouter> getAllMethodRouters(){
        Map<String,MethodRouter> routerMap=new HashMap<>(500);
        MethodRouterDao MethodRouterDao =  SpringContextHandler.getBean("methodRouterDao");
        List<MethodRouter> methodRouters=MethodRouterDao.select();
        if(methodRouters==null){
            return routerMap;
        }

        for(MethodRouter router  :methodRouters){
            String key=router.getOrgCode()+"_"+router.getCallMethod();
            routerMap.put(key,router);
        }
        return routerMap;
    }

    /**
     * 获取公司的所有配置
     * @return
     */
    public static Map<String,MerchantConfig> getAllMerchantConfig(){
        Map<String,MerchantConfig> merchantConfigMap=new HashMap<>(500);
        MerchantConfigDao merchantConfigDao = SpringContextHandler.getBean("merchantConfigDao");
        List<MerchantConfig> merchantConfigList=merchantConfigDao.select();
        if(merchantConfigList==null){
            return merchantConfigMap;
        }

        for(MerchantConfig merchantConfig:merchantConfigList){
            String key=merchantConfig.getOrgCode();
            merchantConfigMap.put(key,merchantConfig);
        }
        return merchantConfigMap;
    }

    /**
     * 根据code->method
     * @param code
     * @return
     */
    public static String getMethdFromEnum(String code){
        String method="";
        for (NotifyScene s : NotifyScene.values()){
            if(code.equals(s.getCode())){
                method=s.getMethod();
                break;
            }
        }
        return method;
    }

    /**
     * 更新最后的通知时间
     * @param messageId
     * @param handleMessage
     */
    public static void updateMerchantMessageLastNotifyTime(String messageId,String handleMessage){
        MerchantMessage updateMerchantMessage=new MerchantMessage();
        updateMerchantMessage.setId(messageId);
        updateMerchantMessage.setHandleMessage(handleMessage);
        updateMerchantMessage.setLastNotifyTime(DateUtils.dateToString(new Date()));
        MerchantMessageDao merchantMessageDao = SpringContextHandler.getBean("merchantMessageDao");
        merchantMessageDao.update(updateMerchantMessage);
    }

    /**
     * 更新消息状态
     * @param messageId
     * @param notifyCount
     * @param notifyFlag
     * @param handleMessage
     */
    public static void updateMerchantMessageState(String messageId,int notifyCount,short notifyFlag,String handleMessage){
        MerchantMessage updateMerchantMessage=new MerchantMessage();
        updateMerchantMessage.setId(messageId);
        updateMerchantMessage.setNotifyCount(notifyCount);
        updateMerchantMessage.setNotifyFlag(notifyFlag);
        updateMerchantMessage.setHandleMessage(handleMessage);
        updateMerchantMessage.setLastNotifyTime(DateUtils.dateToString(new Date()));
        MerchantMessageDao merchantMessageDao = SpringContextHandler.getBean("merchantMessageDao");
        merchantMessageDao.update(updateMerchantMessage);
    }





}
